var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var app = express();
var template = require('art-template');
var utils=require('./utils.js');

//you won't need 'connect-livereload' if you have livereload plugin for your browser
app.use(require('connect-livereload')());
//=============================设置前端模板================================
// view engine setup
template.config('extname', '.html');
app.engine('.html', template.__express);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');

// uncomment after placing your favicon in /public
app.use(favicon(path.join(__dirname, 'public', 'img/favicon.ico')));
app.use(logger('dev'));
app.use(methodOverride());
app.use(bodyParser.json({ type: 'application/*+json' }))
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/static', express.static(path.join(__dirname, 'public')));
app.use(logErrors);
app.use(clientErrorHandler);
app.use(errorHandler);

function logErrors(err, req, res, next) {
  console.error(err.stack);
  next(err);
}
function clientErrorHandler(err, req, res, next) {
  if (req.xhr) {
    res.status(500).send({ error: 'Something blew up!' });
  } else {
    next(err);
  }
}
function errorHandler(err, req, res, next) {
  res.status(500);
  res.render('error', { error: err });
}

//=============================请求路由====================================
// 请求路由
require('./routes.js')(app);

// ========commonController.js=================初始化控制器===================================
// 后端cms
var entrys_cms = utils.getEntry('./controller/cms',true);
var isInCms=utils.checkIsInArray(entrys_cms,'commonController.js');
if(isInCms.isin) entrys_cms.splice(isInCms.index,1);
console.log(entrys_cms);
entrys_cms&&entrys_cms.length&&entrys_cms.forEach(function(val,index){
  require(val)(app);
});

// 前端web
var entrys_web = utils.getEntry('./controller/web',true);
var isInWeb=utils.checkIsInArray(entrys_web,'commonController.js');
if(isInWeb.isin) entrys_web.splice(isInWeb.index,1);
console.log(entrys_web);
entrys_web&&entrys_web.length&&entrys_web.forEach(function(val,index){
  require(val)(app);
});


//==========================异常处理=======================================

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});

module.exports= app 


